package com.qzsoft.zhjd.materiel.modules.materielitem.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.materiel.modules.materielapi.aop.MaterielApiUpdate;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemBaseListDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemBaseSaveDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemBaseSelectDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.entity.MaterielItemBaseEntity;
import com.qzsoft.zhjd.materiel.modules.materielitem.service.MaterielItemBaseService;

/**
 * @ClassName MaterielItemBaseController
 * @Description 养护基地材料字典
 * @author linhuawei
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/api/materielItemBase")
public class MaterielItemBaseController {

    @Autowired
    private MaterielItemBaseService materielItemBaseService;

    /**
     * @Title page
     * @Description 获取基地材料分页列表
     * @param params 查询条件
     * @return 基地材料分页列表
     */
    @RequiresPermissions("materiel:itembase:page")
    @GetMapping("/page")
    public Result<IPage<MaterielItemBaseListDto>> page(@RequestParam Map<String, Object> params) {
        IPage<MaterielItemBaseListDto> page = materielItemBaseService.page(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title getUnbind
     * @Description 获取指定基地未绑定材料列表
     * @param baseCode 基地编号
     * @return 未绑定材料列表
     */
    @GetMapping("/getUnbind")
    public Result<List<MaterielItemBaseSelectDto>> getUnbind(@RequestParam(required = true) String baseCode) {
        List<MaterielItemBaseSelectDto> list = materielItemBaseService.getUnbind(baseCode);
        return ResultUtils.ok(list);
    }

    /**
     * @Title getBind
     * @Description 获取基地材料下拉列表
     * @param baseCode 基地主键
     * @return 基地材料下拉列表
     */
    @GetMapping("/getBind")
    public Result<List<MaterielItemBaseSelectDto>> getBind(@RequestParam(required = true) String baseCode) {
        List<MaterielItemBaseSelectDto> list = materielItemBaseService.getBind(baseCode);
        return ResultUtils.ok(list);
    }

    /**
     * @Title info
     * @Description 获取数据对象
     * @param id 数据主键
     * @return 数据对象
     */
    @RequiresPermissions("materiel:itembase:update")
    @GetMapping("/info/{id}")
    public Result<MaterielItemBaseEntity> info(@PathVariable("id") String id) {
        MaterielItemBaseEntity entity = materielItemBaseService.getById(id);
        if (null == entity) {
            RenExceptionUtils.throwEx(BizCode.DATA_NOT_FOUND, "数据不存在");
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title save
     * @Description 绑定材料字典
     * @param dto 绑定信息
     * @return 绑定结果
     */
    @MaterielApiUpdate("getItemList")
    @RequiresPermissions("materiel:itembase:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MaterielItemBaseSaveDto dto) {
        // 执行添加操作
        ValidatorUtils.validateEntity(dto);
        List<String> errors = materielItemBaseService.saveReturnErrors(dto);

        // 返回结果数据
        Result<String> result = ResultUtils.ok();
        if (null != errors && 0 < errors.size()) {
            result.setMsg(errors.size() + "条数据添加失败");
        }
        return result;
    }

    /**
     * @Title update
     * @Description 修改养护基地材料字典
     * @param entity 材料字典数据
     * @return 修改结果
     */
    @MaterielApiUpdate("getItemList")
    @RequiresPermissions("materiel:itembase:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody MaterielItemBaseEntity entity) {
        ValidatorUtils.validateEntity(entity);
        materielItemBaseService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title delete
     * @Description 删除已经绑定的材料字典
     * @param ids 数据主键
     * @return 删除结果
     */
    @MaterielApiUpdate("getItemList")
    @RequiresPermissions("materiel:itembase:delete")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody String[] ids) {
        if (null != ids && 0 < ids.length) {
            List<String> idList = Arrays.asList(ids);
            materielItemBaseService.removeByIds(idList);
        }
        return ResultUtils.ok();
    }

}
